﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CommonUtil
{
    /// <summary>
    /// 图片处理共通
    /// </summary>
    public static class ImageUtil
    {
        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="b">原始Bitmap</param>
        /// <param name="StartX">开始坐标X</param>
        /// <param name="StartY">开始坐标Y</param>
        /// <param name="iWidth">宽度</param>
        /// <param name="iHeight">高度</param>
        /// <returns>剪裁后的Bitmap</returns>
        public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }

            int w = b.Width;
            int h = b.Height;

            if (StartX >= w || StartY >= h)
            {
                return null;
            }

            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }

            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }

            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);

                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="bitmap">原始Bitmap</param>
        /// <param name="rectangle">矩形大小对象</param>
        /// <returns>剪裁后的Bitmap</returns>
        public static Bitmap KiCut(Bitmap bitmap, Rectangle rectangle)
        {
            return KiCut(bitmap, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
        }

        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="imgFilename">图片路径</param>
        /// <param name="StartX">开始坐标X</param>
        /// <param name="StartY">开始坐标Y</param>
        /// <param name="iWidth">宽度</param>
        /// <param name="iHeight">高度</param>
        /// <returns>剪裁后的图片路径</returns>
        public static string KiCut(string imgFilename, int StartX, int StartY, int iWidth, int iHeight)
        {
            Bitmap bitmap = new Bitmap(imgFilename);
            Bitmap tmpFile = KiCut(bitmap, StartX, StartY, iWidth, iHeight);
            string tmpFilename = getNewFileName();
            tmpFile.Save(tmpFilename);
            return tmpFilename;
        }

        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="imgFilename">图片路径</param>
        /// <param name="rectangle">矩形大小对象</param>
        /// <returns>剪裁后的图片路径</returns>
        public static string KiCut(string imgFilename, Rectangle rectangle)
        {
            return KiCut(imgFilename, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
        }

        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="imgFilename">图片路径</param>
        /// <param name="rectangle">矩形大小对象</param>
        /// <param name="toFilename">目标文件名</param>
        public static void KiCut(string imgFilename, Rectangle rectangle, string toFilename)
        {
            Bitmap bitmap = new Bitmap(imgFilename);
            Bitmap tmpFile = KiCut(bitmap, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
            tmpFile.Save(toFilename);
        }

        /// <summary>
        /// 创建一个新的文件名
        /// </summary>
        /// <returns></returns>
        public static string getNewFileName() {
            string path = Environment.CurrentDirectory + @"\tmp";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var faceImageName = path + @"\" + Guid.NewGuid().ToString() + ".jpg";
            return faceImageName;
        }
    }
}
